﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_Teachers : System.Web.UI.Page
{
    protected string path = string.Empty;
    protected PagedDataSource pds = new PagedDataSource();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["CurrentPage"] = 1;
            ViewState["PageCount"] = 1;

            #region 绑定课程信息
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Course.xml");
            xds.XPath = "/Root/Kind";

            Layer1.DataSource = xds;
            Layer1.DataBind();
            #endregion

            #region 加载培训信息
            CmsDataContext db = new CmsDataContext();

            var ts = (from aT in db.Train
                      where (aT.Started - DateTime.Now).Days >= 0
                      orderby aT.Registed descending
                      select new
                      {
                          ID = aT.ID,
                          Memo = string.Format("{0}将于{1:yyyy-MM-dd HH:mm}在{2}举办", aT.Name, aT.Started, aT.Address),
                          Amended = aT.Amended != null
                      }).Take(6);
            Repeater1.DataSource = ts;
            Repeater1.DataBind();

            var px2 = (from aA in db.Article 
                       where aA.Verified != null 
                       orderby aA.Verified descending 
                       select aA).Take(6);
            Repeater3.DataSource = px2;
            Repeater3.DataBind();
            #endregion

            BindData();
        }
    }

    private void BindData()
    {
        string did = Request.Params["did"];
        string rank = Request.Params["rank"];
        string course = Request.Params["course"];

        CmsDataContext db = new CmsDataContext();
        var teachers = from aT in db.Teacher
                       where aT.Verfied != null
                       select aT;

        if (did == "s")
        {
            teachers = teachers.Where(aT => aT.District == "市直");
            path = "首页-->师资队伍-->市级师资";
        }
        else if (did == "x")
        {
            teachers = teachers.Where(aT => aT.District != "市直");
            path = "首页-->师资队伍-->县级师资";
        }

        if (rank != null && rank != "")
        {
            teachers = teachers.Where(aT => aT.Rank == rank);
        }

        if (course != null && course != "")
        {
            teachers = teachers.Where(aT => aT.Courses.Contains(course));
        }

        pds.DataSource = teachers.OrderBy(aT => aT.Name).ToList();
        pds.PageSize = 10;
        pds.AllowPaging = true;

        pds.CurrentPageIndex =(int)ViewState["CurrentPage"] >= pds.PageCount ? pds.PageCount - 1 : (int)ViewState["CurrentPage"] - 1;

        DataList1.DataSource = pds;
        DataList1.DataBind();

        ViewState["PageCount"] = pds.PageCount;
    }

    protected string GetTopicID(string tName)
    {
        CmsDataContext db = new CmsDataContext();
        var t = from aT in db.Topic where aT.Name.Equals(tName) select aT;
        if (t.Count() != 0)
        {
            return t.First().ID;
        }
        else
        {
            return "";
        }
    }

    protected void Layer1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string v = ((Label)e.Item.FindControl("V1")).Text;
        Repeater l2 = (Repeater)e.Item.FindControl("Layer2");

        XmlDataSource xds = new XmlDataSource();
        xds.DataFile = Toolkit.GetRealDataFilePath("Course.xml");
        xds.XPath = string.Format("/Root/Kind[@name='{0}']/Subject", v);

        l2.DataSource = xds;
        l2.DataBind();
    }

    protected void Layer2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string v = ((Label)e.Item.FindControl("V2")).Text;
        Repeater l3 = (Repeater)e.Item.FindControl("Layer3");

        XmlDataSource xds = new XmlDataSource();
        xds.DataFile = Toolkit.GetRealDataFilePath("Course.xml");
        xds.XPath = string.Format("/Root/Kind/Subject[@name='{0}']/Course", v);

        l3.DataSource = xds;
        l3.DataBind();
    }
    protected void ChangePage(object sender, ImageClickEventArgs e)
    {
        if (sender == FirstButton)
        {
            ViewState["CurrentPage"] = 1;
        }
        else if (sender == PreviousButton)
        {
            ViewState["CurrentPage"] = (int)ViewState["CurrentPage"] == 1 ? 1 : (int)ViewState["CurrentPage"] - 1;
        }
        else if (sender == NextButton)
        {
            ViewState["CurrentPage"] = (int)ViewState["CurrentPage"] == (int)ViewState["PageCount"] ? (int)ViewState["PageCount"] : (int)ViewState["CurrentPage"] + 1;
        }
        else if (sender == LastButton)
        {
            ViewState["CurrentPage"] = (int)ViewState["PageCount"];
        }

        BindData();
    }
}